package org.kl.wx.entity;

import java.io.File;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;

import org.hibernate.validator.constraints.NotBlank;
import org.kl.bf.entity.AuditableEntity;
import org.kl.bf.entity.Excel;

/**
 * 微信公共账号
 */

@Entity
@Table(name = "wx_app")
public class App extends AuditableEntity implements WxMpConfigStorage {
	private static final long serialVersionUID = 1L;

	@Excel(exportName = "应用ID")
	private String originAppId;
	@Excel(exportName = "应用名称")
	private String appName;
	@Excel(exportName = "微信号")
	private String appNo;
	@Excel(exportName = "账号类型")
	private String appType;
	@Excel(exportName = "介绍")
	private String appDesc;
	@Excel(exportName = "认证情况")
	private Integer isAuth;
	@Excel(exportName = "地址")
	private String address;
	@Excel(exportName = "登陆邮箱")
	private String loginEmail;
	@Excel(exportName = "应用密钥")
	private String appSecret;
	@Excel(exportName = "appToken")
	private String appToken;
	@Excel(exportName = "aesKey")
	private String aesKey;
	@Excel(exportName = "accessToken")
	private String accessToken;
	@Excel(exportName = "expiresTime")
	private long expiresTime = 0;
	@Excel(exportName = "jsapiTicket")
	private String jsapiTicket;
	@Excel(exportName = "jsapiTicketExpiresTime")
	private long jsapiTicketExpiresTime;
	@Excel(exportName = "oauth2redirectUri")
	private String oauth2redirectUri;

	public App() {
	}

	public App(String id) {
		this.id = id;
	}

	@NotBlank
	public String getOriginAppId() {
		return originAppId;
	}

	public void setOriginAppId(String originAppId) {
		this.originAppId = originAppId;
	}

	@NotBlank
	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	@NotBlank
	public String getAppNo() {
		return appNo;
	}

	public void setAppNo(String appNo) {
		this.appNo = appNo;
	}

	@NotBlank
	public String getAppType() {
		return appType;
	}

	public void setAppType(String appType) {
		this.appType = appType;
	}

	@NotBlank
	public String getAppDesc() {
		return appDesc;
	}

	public void setAppDesc(String appDesc) {
		this.appDesc = appDesc;
	}

	public Integer getIsAuth() {
		return isAuth;
	}

	public void setIsAuth(Integer isAuth) {
		this.isAuth = isAuth;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@NotBlank
	public String getLoginEmail() {
		return loginEmail;
	}

	public void setLoginEmail(String loginEmail) {
		this.loginEmail = loginEmail;
	}

	@NotBlank
	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getAppToken() {
		return appToken;
	}

	public void setAppToken(String appToken) {
		this.appToken = appToken;
	}

	public String getAesKey() {
		return aesKey;
	}

	public void setAesKey(String aesKey) {
		this.aesKey = aesKey;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public long getExpiresTime() {
		return expiresTime;
	}

	public void setExpiresTime(long expiresTime) {
		this.expiresTime = expiresTime;
	}

	public long getJsapiTicketExpiresTime() {
		return jsapiTicketExpiresTime;
	}

	public void setJsapiTicketExpiresTime(long jsapiTicketExpiresTime) {
		this.jsapiTicketExpiresTime = jsapiTicketExpiresTime;
	}

	public void setJsapiTicket(String jsapiTicket) {
		this.jsapiTicket = jsapiTicket;
	}

	public String getOauth2redirectUri() {
		return oauth2redirectUri;
	}

	public void setOauth2redirectUri(String oauth2redirectUri) {
		this.oauth2redirectUri = oauth2redirectUri;
	}

	@Transient
	public boolean isAccessTokenExpired() {
		return System.currentTimeMillis() > this.expiresTime;
	}

	public synchronized void updateAccessToken(WxAccessToken accessToken) {
		updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
	}

	public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
		this.accessToken = accessToken;
		this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
	}

	public void expireAccessToken() {
		this.expiresTime = 0;
	}

	public String getJsapiTicket() {
		return jsapiTicket;
	}

	@Transient
	public boolean isJsapiTicketExpired() {
		return System.currentTimeMillis() > this.jsapiTicketExpiresTime;
	}

	public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
		this.jsapiTicket = jsapiTicket;
		// 预留200秒的时间
		this.jsapiTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
	}

	public void expireJsapiTicket() {
		this.jsapiTicketExpiresTime = 0;
	}

	@Transient
	public String getAppId() {
		return this.originAppId;
	}

	@Transient
	public String getSecret() {
		return this.appSecret;
	}

	@Transient
	public String getToken() {
		return this.appToken;
	}

	@Transient
	public String getHttp_proxy_host() {
		return "";
	}

	public void setHttp_proxy_host(String http_proxy_host) {
	}

	@Transient
	public int getHttp_proxy_port() {
		return 0;
	}

	public void setHttp_proxy_port(int http_proxy_port) {
	}

	@Transient
	public String getHttp_proxy_username() {
		return null;
	}

	public void setHttp_proxy_username(String http_proxy_username) {
	}

	@Transient
	public String getHttp_proxy_password() {
		return null;
	}

	public void setHttp_proxy_password(String http_proxy_password) {
	}

	@Override
	@Transient
	public String getPartnerId() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Transient
	public String getPartnerKey() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Transient
	public File getTmpDirFile() {
		// TODO Auto-generated method stub
		return null;
	}

}
